﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Products;
using Products.DataAcccess;
using Products.Databases;
using Products.Databases.DataAccess;

namespace Ornament.MVCWebFrame.Areas.DataBase.Controllers
{
    public class TableController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="product">name of product</param>
        /// <param name="version"></param>
        /// <returns></returns>
        public ActionResult Index(string product, string version)
        {
            //get product
            Product productIns = (new ProductDao()).Get(product);
            ViewData["Product"] = productIns;

            //get the pVersion which you want to switch.
            ProductVersion pVersion =
                version == null
                    ? productIns.Versions[0]
                    : productIns.GetVersion(version);
            ViewData["CurrentVersion"] = pVersion;

            IList<Table> tables = (new TableAccess()).GetTables(productIns);
            foreach (Table table in tables)
            {
                table.SwitchTo(pVersion);
            }

            return View(tables);
        }

        //
        // GET: /Table/Details/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name">Table's conceptName Name</param>
        /// <param name="version"></param>
        /// <returns></returns>
        public ActionResult Details(string name, string version)
        {
            Table tableIns = (new TableAccess()).Get(name);
            ProductVersion versionObj = version == null
                                            ? tableIns.Product.GetVersions(tableIns)[0]
                                            : tableIns.Product.GetVersion(version);
            tableIns.SwitchTo(versionObj);
            IList<Field> fieldList = (new FieldDao()).Find(tableIns);
            foreach (Field field in fieldList)
            {
                field.SwitchTo(versionObj);
            }
            ViewData["Fields"] = fieldList;
            ViewData["Product"] = tableIns.Product;
            ViewData["CurrentVersion"] = versionObj;
            return View(tableIns);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Details(Table table)
        {
            return RedirectToAction("index");
        }

        //
        // GET: /Table/Create

        public ActionResult Create(Guid? projectId)
        {
            ViewData["Product"] = (new ProductDao()).Get(projectId.Value);
            return View();
        }

        //
        // POST: /Table/Create

        [HttpPost]
        public ActionResult Create(Table table)
        {
            try
            {
                var existTable = (new TableAccess()).Get(table.ConceptName);

                if (existTable != null)
                {
                    this.ModelState.AddModelError("ConceptName", "Duplicate table concept name,please try another one.");
                }

                if (ModelState.IsValid)
                {
                    (new TableAccess()).SaveOrUpdate(table);
                    return RedirectToAction("Details", "Table", new { id = table.Product.Id });
                }
                ViewData["Product"] = table.Product;
                return View(table);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Table/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Table/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Table/Delete/5
        public ActionResult Delete(Guid? id)
        {
            var tableAccess = new TableAccess();
            Table table = tableAccess.Get(id.Value);
            tableAccess.Delete(table);
            return RedirectToAction("Index");
        }
    }
}